<?php
// +----------------------------------------------------------------------
// | Iecms [ Make the most exquisite CMS ]
// +----------------------------------------------------------------------
// | Copyright (c) 2009~2019 http://www.iemoo.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: iemoo <i@iemoo.com>
// +----------------------------------------------------------------------
// | DateTime: 2019/11/19 11:08
// +----------------------------------------------------------------------
namespace app\admin\controller;

use app\common\model\Category as CategoryModel;
use app\common\model\Module as ModuleModel;

use app\admin\validate\Category as CategoryValidate;
use think\exception\ValidateException;

use think\facade\View;
use think\facade\Request;

/**
 * 栏目
 * Class AuthGroup
 */
class Category extends Base
{
	
	public function __construct()
    {
        parent::__construct();

		$category_list	= CategoryModel::order('sort desc,id asc')->select();
		$category_list	= $category_list->toArray();
        $this->category_list_tree = tree_cate($category_list);
	
		
	 }

    /**
     * 栏目列表
     * @return mixed
     */
    public function index()
    {
		View::assign('category_list_tree',$this->category_list_tree);
		return View::fetch('index');
    }

    /**
     * 添加栏目
     * @return mixed
     */
    public function add()
    {
        if (Request::isPost()) {

			$data	= Request::only(['pid', 'module_id', 'title', 'name', 'icon', 'keywords', 'description', 'url', 'type', 'template_list', 'template_show', 'sort', 'status']);

            try {
				validate(CategoryValidate::class)->check($data);
			} catch (ValidateException $e) {
				// 验证失败 输出错误信息
				$this->error($e->getError());
			}

			if (CategoryModel::create($data)) {
				$this->success('保存成功',url('admin/Category/index'));
			} else {
				$this->error('保存失败');
			}

        }

		//获取所有模版
        $template_list = get_template();

		$module_list = ModuleModel::select();		
		View::assign('module_list',$module_list);
		
		View::assign('category_list_tree',$this->category_list_tree);

		View::assign('template_list',$template_list);

		return View::fetch('add');
    }

    /**
     * 编辑栏目
     * @param $id
     * @return mixed
     */
    public function edit($id)
    {

		if (Request::isPost()) {

			$data	= Request::only(['id', 'pid', 'module_id', 'title', 'name', 'icon', 'keywords', 'description', 'url', 'type', 'template_list', 'template_show', 'sort', 'status']);
			
            try {
				validate(CategoryValidate::class)->check($data);
			} catch (ValidateException $e) {
				// 验证失败 输出错误信息
				$this->error($e->getError());
			}

			if (CategoryModel::update($data, ['id' => $id])) {
				$this->success('保存成功',url('admin/Category/index'));
			} else {
				$this->error('保存失败');
			}

        }


        $info = CategoryModel::find($id);
		$module_list = ModuleModel::select();

		//排序子分类
		$category_list = category_remove_son($this->category_list_tree,$info->id);


		//获取所有模版
        $template_list = get_template();
		
		View::assign('info',$info);
		View::assign('module_list',$module_list);		
		View::assign('category_list',$category_list);

		View::assign('category_list_tree',$this->category_list_tree);

		View::assign('template_list',$template_list);
		return View::fetch('edit');
    }


    /**
     * 删除栏目
     * @param $id
     */
    public function delete($id)
    {
		
		$category = CategoryModel::find($id);

        if ($category->delete()) {
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }

  
	/**
     * 快捷更新
     */
    public function setStatus(){
	
        if (Request::isGet()) {
			$data = Request::only(['id', 'is_menu', 'status', 'sort']);

            if (CategoryModel::update($data) !== false) {
                $this->success('更新成功');
            } else {
                $this->error('更新失败');
            }
        }
    }

}